﻿/*
--------------------------------------
File: CropCharacters.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A algorithm which crops the letters in the license plate, returns a bitmap array with the characters
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    public class CropCharacters: ColorInterface
    {
        /// <summary>
        /// Cuts the edges surrounding the license plate, also deletes noise that might left over
        /// Creates a bitmap array with the individual characters in the license plate
        /// </summary>
        /// <param name="sourceArray">a reference to the imageArray</param>
        /// <param name="width">the width of the cropped image</param>
        /// <param name="height">the height of the cropped image</param>
        /// <returns>a bitmap array with the individual characters in the license plate</returns>
        public Bitmap[] DoAlgorithm(ref int[] sourceArray, int width, int height)
        {
            //The dimensions
            int left_top = 0,
                right_top = width - 1,
                left_bottom = (height - 1) * width,
                right_bottom = height * width - 1;

            //Resets all the static class variables used in BlobUtils
            BlobUtils.initialize();
            
            //Cuts the edges surrounding the license plate
            //Filters the top row
            for (int i = left_top; i <= right_top; i++)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, BLACK, WHITE, width, i);
            }
            //filter the bottom row
            for (int i = left_bottom; i <= right_bottom; i++)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, BLACK, WHITE, width, i);
            }
            //Filter first column
            for (int i = left_top; i <= left_bottom; i += width)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, BLACK, WHITE, width, i);
            }
            //Filter last column
            for (int i = right_top; i <= right_bottom; i += width)
            {
                BlobUtils.colorTargetBlob(ref sourceArray, BLACK, WHITE, width, i);
            }

            //Resets all the static class variables used in BlobUtils
            BlobUtils.initialize();
            //Finds the characters (and noise) in the license plate
            BlobUtils.findBlobs(ref sourceArray, BLACK, width);
            //Filters noise blobs
            BlobUtils.filterSmallBlobs(sourceArray.Length * 0.002);
            //Creates a bitmap array for all of the character blobs
            return BlobUtils.getCharacters(ref sourceArray, width).ToArray();
        }
    }
}






